import 'package:cbec_app/pages/home/controller/home_ctr.dart';
import 'package:cbec_app/pages/home/view/decoration_renderer.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../../components/common/recommend.dart';
import '../../../entity/home/hom_nav_tab_entity.dart';
import '../../../utils/AppData.dart';
import '../../../utils/EventMgr.dart';
import '../../main/controller/main_ctr.dart';

///Tab
class SegmentTabPage extends GetView<HomeController> {
  const SegmentTabPage({super.key});

  @override
  Widget build(context) {
    if (controller.contentNavEntity == null) {
      return const SizedBox();
    }

    ContentNavEntity contentNavEntity = controller.contentNavEntity!;

    return GetBuilder<HomeController>(
      builder: (controller) {
        return const Column(
          children: [
            DecorationRenderer(),
            // Recommend(location: "MemberCenter", isShowTitle: false),
          ],
        );
      },
    );
  }

  Widget _buildTopTabsWidget(HomeController controller) {
    if (controller.contentNavEntity == null) {
      return const SizedBox();
    }
    ContentNavEntity contentNavEntity = controller.contentNavEntity!;
    return Container(
      decoration: BoxDecoration(
        color: Tools.fromHex(contentNavEntity.bg ?? ''),
      ),
      child: Row(
        children: [
          controller.tabCtr != null
              ? Expanded(
                  child: _Tabs(
                    controller.tabTitles,
                    controller,
                  ),
                )
              : const SizedBox(),
          const SizedBox(width: 10), // 添加间隔
          buildRightIconText(controller),
        ],
      ),
    );
  }

  Widget buildRightIconText(HomeController controller) {
    if (controller.contentNavEntity == null) {
      return const SizedBox();
    }
    ContentNavEntity contentNavEntity = controller.contentNavEntity!;

    return Visibility(
      visible: controller.tabTitles.isNotEmpty,
      child: IconButton(
        onPressed: () {
          EventMgr.send(EventType.exchangeNav, MainController.mainBottomNavRouteNameClassify);
        },
        icon: Row(
          mainAxisSize: MainAxisSize.min, // 根据内容大小调整
          children: [
            Image.asset(
              'assets/images/fenlei.png',
              color: Tools.fromHex(contentNavEntity.opentureColor ?? ''),
              width: 20, // 设置图片宽度
              height: 20, // 设置图片高度
            ),
            const SizedBox(width: 5), // 图片和文本之间的间距
            Text(
              '分类',
              style: TextStyle(color: Tools.fromHex(contentNavEntity.opentureColor ?? ''), fontSize: (contentNavEntity.titleSize!.toDouble() / 2).sp),
            ),
          ],
        ),
      ),
    );
  }
}

Widget _Tabs(List<String> titles, HomeController controller) {
  if (controller.contentNavEntity == null) {
    return const SizedBox();
  }
  ContentNavEntity contentNavEntity = controller.contentNavEntity!;
  return Container(
    // color: Tools.fromHex(contentNavEntity.bg ?? ''),
    child: TabBar(
      isScrollable: true,
      dividerColor: Colors.transparent, // 移除分隔线颜色
      indicator: const BoxDecoration(), // 清空默认指示器样式
      labelPadding: const EdgeInsets.symmetric(horizontal: 10), // 调整标签左右间距
      labelColor: Tools.fromHex(contentNavEntity.activeTitleColor ?? ''),
      labelStyle: TextStyle(fontSize: (contentNavEntity.activeTitleSize!.toDouble() / 2).sp),
      unselectedLabelColor: Tools.fromHex(contentNavEntity.titleColor ?? ''),
      unselectedLabelStyle: TextStyle(fontSize: (contentNavEntity.titleSize!.toDouble() / 2).sp),
      controller: controller.tabCtr,
      tabAlignment: TabAlignment.start,
      tabs: titles.map((item) => Tab(text: item)).toList(),
    ),
  );
}
